<?php
/**
 * @author GhostWei
 * @date Aug 11, 2012
 */
class controller_www_ajax extends front_controller_ajax {
	
	public function action_index() {
		$this->render();
	}
	
	public function action_comment() {
		if(!$this->getRequest()->isPost()) {
			$this->_error('INVALID REQUEST');
		}
		
		$parent_id = 0;
		$submit = $this->_post('submit');
		$category = trim($this->_post('category'));
		if($this->_post('action') == 'photo' || $this->_post('action') == 'gallery') {
			$category = 'photo';
		}
		
		try {
			$type = new community_review_category($category);
		} catch (Exception $e) {
			$this->_error($this->language('参数错误'));
		}
		
		$concept = new community_review_concept($this->lang);

		if($submit == 'like') {
			try {
				$where = array('category = ?' => $type->getId(), 'is_approved = 1', 'id = ?' => intval($this->_post('comment_id')));
				if(!($row = $concept->fetch($where))) {
					$this->_error($this->language('这条评论不存在'));
				}
				$row->setLike($row->getLike() + 1);
				$row->save();				
				$this->_data->message = $this->language('评论发表成功，谢谢您的参与');
				$this->_output();	
			} catch (Exception $e) {
				$this->_error($this->language('提交失败') . ': ' . $e->getMessage());
			}			
		}
		
		if($submit == 'submit') {
			try {
				$title = '';
				if($type->is('special')) {
					if(1 && $special = $this->cache()->special()->getInformation(array('name' => trim($this->_post('name'))))) {
						$parent_id = $special['id'];
						$title = 'Re:' . $special['title'];
					} else {
						$this->_error($this->language('该专辑不存在'));
					}
				}
				if($type->is('photo')) {
					if(1 && $photo = $this->cache()->photo()->getIndex(array('id' => trim($this->_post('id'))))) {
						$parent_id = $photo['id'];
						$title = 'Re:';
					} else {
						$this->_error($this->language('该图片不存在'));
					}
				}
				
				$row = $concept->create();
				$row->setCategory($type->getId());
				$row->setParentId($parent_id);
				if($this->user) {
					$row->setPassportId($this->user->getId());
				} else {
					$row->setPassportId(0);
				}
				$row->setReplyId(intval($this->_post('reply_id')));
				$row->setTitle($title);
				$row->setMessage($this->_post('message'));
				$row->setIsApproved(1);
				$row->setIp($this->getRequest()->getClientIp(true));
				$row = $row->save();
				$this->_data->id = $row->getId();
				$this->_data->message = $this->language('评论发表成功，谢谢您的参与');
				$this->_output();	
			} catch (Exception $e) {
				$this->_error($this->language('提交失败') . ': ' . $e->getMessage());
			}			
		}

		if($type->is('special') && $special = $this->cache()->special()->getInformation(array('name' => trim($this->_post('name'))))) {
			$parent_id = $special['id'];
		}

		if($type->is('photo') && $photo = $this->cache()->photo()->getInformation(array('id' => trim($this->_post('id'))))) {
			$parent_id = $photo['id'];
		}
		$where = array('category = ?' => $type->getId(), 'parent_id = ?' => $parent_id, 'is_approved = 1', 'reply_id = 0');
		$page = intval($this->_post('page', 1));
		
		$adapter = new system_paginator_adapter_concept($concept);
		$adapter->setCurrentPageNumber($page);
		$adapter->setLimit(10);
		$adapter->where($where);
		$adapter->order('id ASC');
		$list = new system_paginator($adapter);
		$ids = $reply = $uids = $user = array();
		foreach($list AS $row) {
			$ids[] = $row->getId();
			if($row->getPassportId() > 0) {
				$uids[$row->getPassportId()] = $row->getPassportId();
			}
		}
		if(!empty($ids)) {
			$id = implode(', ', $ids);
			foreach($concept->fetchAll(array("reply_id IN($id)")) AS $row) {
				$reply[$row->getReplyId()][] = $row;
				if($row->getPassportId() > 0) {
					$uids[$row->getPassportId()] = $row->getPassportId();
				}
			}
		}
		if(!empty($uids)) {
			$id = implode(', ', $uids);
			$uConcept = new community_member_concept($this->lang);
			foreach($uConcept->fetchAll(array("member.passport_id IN($id)")) AS $row) {
				$user[$row->getId()] = $row;
			}
		}
		
		$this->_data->html = $this->view->partial(
				'ajax/commentList.phtml', 
				array(
						'list' => $list, 
						'reply' => $reply, 
						'user' => $user, 
						'page' => $page
				)
		);
		$this->_output();
	}
	
	public function action_background() {
		$dimension = 1024 * 768;
		$sql = "SELECT a.`hash` FROM `resource_file` AS a, `picture_photo` AS b WHERE 
				a.`hash` = b.`hash` AND b.`is_active` = 1 AND a.`type` = '0' AND a.`dimension` >= $dimension ORDER BY RAND() LIMIT 1";
		if(!($hash = system_db::getAdapter()->fetchOne($sql))) {
			$this->_error('No Photo');
		}
		$url = new resource_display($hash);
		$this->_data->url = $url->getOriginal(false);
		$this->_output();
	}
	
}
